package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.BedroomLevel;
import com.teacher.pojo.entity.PartySchool;
import com.teacher.pojo.enums.BedroomLevelType;
import com.teacher.pojo.excel.BedroomLevelExcel;
import com.teacher.pojo.excel.PartySchoolExcel;
import com.teacher.service.IBedroomLevelService;
import com.teacher.service.IPartySchoolService;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelPartySchoolListener extends AnalysisEventListener<PartySchoolExcel> {
    private Long termId;
    private IPartySchoolService iPartySchoolService;

    private List<PartySchool> partySchoolList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelPartySchoolListener(Long termId, IPartySchoolService iPartySchoolService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iPartySchoolService = iPartySchoolService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(PartySchoolExcel partySchoolExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(partySchoolExcel.getStudentKey())) {
            PartySchool partySchool = new PartySchool();
            BeanUtils.copyProperties(partySchoolExcel, partySchool);
            partySchool.setTermId(termId);
            partySchoolList.add(partySchool);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iPartySchoolService.saveBatch(partySchoolList);
    }
}
